# Maintainer: Sarah Ottinger

_realname=click
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=8.2.1
pkgrel=1
pkgdesc='A simple wrapper around optparse for powerful command line utilities (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://click.palletsprojects.com/"
msys2_repository_url='https://github.com/pallets/click'
msys2_references=(
  'purl: pkg:pypi/click'
)
license=('spdx:BSD-3-Clause')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-colorama"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-flit-core"
  "${MINGW_PACKAGE_PREFIX}-python-myst-parser"
  "${MINGW_PACKAGE_PREFIX}-python-pallets-sphinx-themes"
  "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-log-cabinet"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-issues"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-tabs"
)
source=("https://github.com/pallets/click/archive/${pkgver}/${_realname}-$pkgver.tar.gz")
sha512sums=('420ba1989410af0949317f9651c6b292da68605b86b4cf0e2382eaba1defbc9eb4b9d97e0a71e5cdb51a61a1f578e81fd864e1abb0cdcd0a8ee26a899c802afe')

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation

  # Forward installation for building documentation
  _pkgdir="${srcdir}/python-install-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${_pkgdir}" dist/*.whl

  msg "Build documentation"
  cd "${srcdir}/python-build-${MSYSTEM}/docs"
  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
  PYTHONPATH="${_pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages" \
    make html
}

package_python-click() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.txt"
}

package_python-click-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/docs/_build/html" "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
